<?php
class Member extends ARMember {
	public $autoLogin;
	public $confirmPassword;
	public $verifyCode;
	protected $_identity;
	protected $_authenticateMethod;

	public function rules () {
		return array(
			array(
				'username, password',
				'required',
				'on' => 'login' 
			),
			array(
				'username',
				'usernameJudge',
				'on' => 'login' 
			),
			array(
				'email, password, username, confirmPassword',
				'required',
				'on' => 'register' 
			),
			
			array(
				'email',
				'email' 
			),
			array(
				'email',
				'length',
				'min' => 6,
				'max' => 40 
			),
			array(
				'username',
				'length',
				'min' => 4,
				'max' => 15 
			),
			array(
				'password',
				'length',
				'min' => 4,
				'max' => 32 
			),
			
			array(
				'email, username',
				'checkExists',
				'on' => 'register' 
			),
			array(
				'confirmPassword',
				'compare',
				'compareAttribute' => 'password',
				'message' => '密码不匹配',
				'on' => 'register' 
			),
			
			array(
				'password',
				'authenticate',
				'on' => 'login' 
			),
			array(
				'autoLogin',
				'safe' 
			),
			
			array(
				'registerTime',
				'default',
				'value' => BEGIN_TIME,
				'setOnEmpty' => false,
				'on' => 'insert, register' 
			) 
		);
	}

	public function attributeLabels () {
		static $labels;
		return empty($labels) ? $labels = array(
			'username' => '昵称',
			'email' => '邮箱',
			'password' => '密码',
			'avatar' => '头像',
			
			'confirmPassword' => '确认密码',
			'autoLogin' => '自动登录' 
		) : $labels;
	}

	public function usernameJudge ($attribute, $params) {
		if (preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $this->$attribute)) {
			$this->_authenticateMethod = 'email';
			$this->email = $this->username;
			unset($this->username);
		} else
			$this->_authenticateMethod = 'username';
	}

	public function authenticate ($attribute, $params) {
		if ($this->_authenticateMethod == 'email')
			$this->_identity = new UserIdentity($this->email, $this->password, 'email');
		else
			$this->_identity = new UserIdentity($this->username, $this->password, 'username');
		
		if (!$this->_identity->authenticate()) {
			if (in_array($this->_identity->errorCode, array(
				UserIdentity::ERROR_EMAIL_INVALID,
				UserIdentity::ERROR_USERNAME_INVALID 
			)))
				$this->addError('username', '账号不存在');
			elseif ($this->_identity->errorCode == UserIdentity::ERROR_PASSWORD_INVALID)
				$this->addError('password', '帐号或密码不正确');
			else
				$this->addError('password', '未知错误');
		}
	}

	public function checkExists ($attribute, $params) {
		if ($attribute == 'email') {
			$exists = $this->findByAttributes(array(
				'email' => $this->$attribute,
				'emailStatus' => 1 
			));
			if ($exists)
				$this->addError($attribute, '邮箱已经有人使用了,换一个试试');
		} else {
			$exists = $this->findByAttributes(array(
				'username' => $this->$attribute 
			));
			if ($exists)
				$this->addError($attribute, '昵称已经有人使用了,换一个试试');
		}
	}

	protected function beforeSave () {
		if (in_array($this->getScenario(), array(
			'insert',
			'update',
			'register' 
		))) {
			$this->password = $this->hashPassword($this->password);
		}
		return true;
	}

	public function register () {
		$this->setScenario('register');
		if ($this->save()) {
			if (!$this->emailCheck($this->email)) {
				$errorMessage = 'uid:' . $this->uid . ' email:' . $this->email . ' check error:' . Yii::app()->mail->ErrorInfo;
				Yii::log($errorMessage, 'error', 'Member.register');
			}
			return true;
		}
		return false;
	}

	public function login () {
		$this->setScenario('login');
		if ($this->validate()) {
			$duration = $this->autoLogin ? 3600 * 24 * 30 : 0;
			$user = Yii::app()->user;
			$user->allowAutoLogin = true;
			$user->login($this->_identity, $duration);
			return true;
		}
		return false;
	}

	public function emailCheck ($email) {
		$mail = Yii::app()->mail;
		$mail->AddAddress($email);
		$mail->ErrorInfo = '';
		$mail->Subject = '来自' . Yii::app()->name . '的注册确认信';
		$mail->Body = '这是来自' . Yii::app()->name . '的注册确认信<br>这就确认了哈';
		return $mail->Send();
	}

	public function hashPassword ($password) {
		return md5($password);
	}
}
?>